package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Category;
import com.itheima.domain.CategoryExample;
import com.itheima.mapper.CategoryMapper;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    /**
     * 分页查询
     */
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public PageInfo<Category> findByPage(Integer pageNum, Integer pageSize) {
        //查询单页数据
        //设置分页查询条件
        PageHelper.startPage(pageNum,pageSize);
        // 创建查询对象 和查询条件 order_no基于查询
        // order_no desc  , 升序asc   这个得写判断的上边下边基于上边的按sql语句
        CategoryExample categoryExample = new CategoryExample();
        // 这里是可以的排序记得要进行多测试
        categoryExample.setOrderByClause("order_no asc");
         // 基于查询所有的条件id查询所有
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        //进行数据的封装使用pageInfo,然后进行对象的返回
        return new PageInfo<Category>(categories);



    }

    /**
     * 种类保存
     * @param category
     */
    @Override
    public void saveCategory(Category category) {
//        这里是进行改造的的
        System.out.println(category);
        categoryMapper.insertSelective(category);

    }

    /**
     * 回显示数据
     * @param id
     * @return
     */
    @Override
    public Category findCategoryById(Integer id) {
        Category category = categoryMapper.selectByPrimaryKey(id);
//        Category categoryById = categoryMapper.findCategoryById(id);
        return category;
    }

    /**
     * 修改 两种写发
     * @param category
     */

    @Override
    public void upDate(Category category) {
        categoryMapper.updateByPrimaryKeySelective(category);
//        categoryMapper.update(category);
    }

    @Override
    public void deleteById(Integer id) {
        categoryMapper.deleteByPrimaryKey(id);
//        categoryMapper.deleteById(id);
    }

    /**
     * 删除所有
     * @param ids
     */
    @Override
    public void deleteSelect(Integer[] ids) {
        for (Integer id : ids) {
       categoryMapper.deleteByPrimaryKey(id);
        }
    }
    /**  分页查询其实就是两个的结合
     *  @Autowired
     *     private CategoryMapper categoryMapper;
     *
     *     @Override
     *     public List<Category> findAll() {
     *         //创建查询对象
     *         CategoryExample example = new CategoryExample();
     *
     *         //设置条件  升序排序
     *         example.setOrderByClause("order_no asc");
     *
     *         return categoryMapper.selectByExample(example);
     *     }
     *
     *     //分页查询
     *     @Override
     *     public PageInfo<Category> findByPage(Integer pageNum, Integer pageSize) {
     *         PageHelper.startPage(pageNum, pageSize);
     *
     *         List<Category> list = categoryMapper.selectByExample(null);
     *
     *         return new PageInfo<>(list);
     *     }
     */

}

